﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LLClient.SaveAndLoad;

namespace LLClient.Battle
{
    /// <summary>
    /// Interaction logic for ReplayWindow.xaml
    /// </summary>
    public partial class ReplayWindow : Window
    {
        #region Constant Values
        private const int N_LINES = 10;
        private const int BLOCK_SIZE = 30;
        private const int FIELD_SIZE = 50;
        private const int PATROL = 1;
        private const int SUBMARINE = 2;
        private const int DESTROYER = 3;
        private const int BATTLESHIP = 4;
        private const int CARRIER = 5;
        #endregion

        LLFile scenario;
        int step;
        public ReplayWindow()
        {
            InitializeComponent();
        }

        public ReplayWindow(LLFile scenario)
        {
            InitializeComponent();
            this.scenario = scenario;
            player1Label.Content = scenario.Captain1Name;
            player2Label.Content = scenario.Captain2Name;
            drawInitGrid();
            step = 0;
        }

        private void nextButton_Click(object sender, RoutedEventArgs e)
        {
            if (step < scenario.Turns.Count)
            {
                TurnSerialized t = scenario.Turns[step];
                string position = "";
                position += (char)(t.Target_tile / 10 + 65);
                position += (t.Target_tile % 10 + 1).ToString();
                string shipName = "";
                int ship = -1;

                if (t.Cpt_name == player1Label.Content.ToString()) // player 1 turn
                {
                    ship = drawShot(true, t.Target_tile, scenario.Field1.ContainsKey(t.Target_tile));
                }
                else
                {
                    ship = drawShot(false, t.Target_tile, scenario.Field2.ContainsKey(t.Target_tile));
                }

                switch (ship)
                {
                    case PATROL:
                        shipName = "Patrol Ship";
                        break;
                    case DESTROYER:
                        shipName = "Destroyer";
                        break;
                    case SUBMARINE:
                        shipName = "Submarine";
                        break;
                    case BATTLESHIP:
                        shipName = "Battleship";
                        break;
                    case CARRIER:
                        shipName = "Carrier";
                        break;
                }

                if (ship > 0)
                    logBox.Text += t.Cpt_name + " shot " + shipName + " at " + position + ".\n";
                else
                    logBox.Text += t.Cpt_name + " missed shot at " + position + ".\n";

                step++;
                if (step == scenario.Turns.Count)
                {
                    logBox.Text += t.Cpt_name + " won the battle\n";
                    nextButton.IsEnabled = false;
                }
            }
            else
            {

            }
        }

        private int drawShot(bool isPlayer1, int target, bool isHit)
        {
            if (isHit)
            {
                int ship = -1;
                if (isPlayer1)
                {
                    ship = scenario.Field1[target];
                }
                else
                {
                    ship = scenario.Field2[target];
                }
                UserControl ucShip = new UserControl();

                if (isHit)
                    ucShip = new FlagHitControl();
                else
                    ucShip = new FlagMissedControl();
                
                ucShip.Margin = new Thickness((target % 10) * 30, (target / 10) * 30, 0, 0);
                if (isPlayer1)
                    gridPlayer2.Children.Add(ucShip);
                else
                    gridPlayer1.Children.Add(ucShip);

                return ship;
            }
            else
            {
                return -1;
            }
        }

        private void drawInitGrid()
        {
            // draw vertical lines
            for (int i = 1; i < N_LINES; i++)
            {
                Line line = new Line();

                line.Stroke = Brushes.Black;
                line.Fill = Brushes.Black;
                line.StrokeThickness = 1;
                line.X1 = i * BLOCK_SIZE;
                line.X2 = line.X1;

                line.Y1 = 0;
                line.Y2 = gridPlayer1.Height;

                gridPlayer1.Children.Add(line);
            }
            for (int i = 1; i < N_LINES; i++)
            {
                Line line = new Line();

                line.Stroke = Brushes.Black;
                line.Fill = Brushes.Black;
                line.StrokeThickness = 1;
                line.X1 = i * BLOCK_SIZE;
                line.X2 = line.X1;

                line.Y1 = 0;
                line.Y2 = gridPlayer2.Height;

                gridPlayer2.Children.Add(line);
            }

            // draw horizontal lines
            for (int i = 1; i < N_LINES; i++)
            {
                Line line = new Line();

                line.Stroke = Brushes.Black;
                line.Fill = Brushes.Black;
                line.StrokeThickness = 1;
                line.Y1 = i * BLOCK_SIZE;
                line.Y2 = line.Y1;

                line.X1 = 0;
                line.X2 = gridPlayer1.Width;

                gridPlayer1.Children.Add(line);
            }
            for (int i = 1; i < N_LINES; i++)
            {
                Line line = new Line();

                line.Stroke = Brushes.Black;
                line.Fill = Brushes.Black;
                line.StrokeThickness = 1;
                line.Y1 = i * BLOCK_SIZE;
                line.Y2 = line.Y1;

                line.X1 = 0;
                line.X2 = gridPlayer2.Width;

                gridPlayer2.Children.Add(line);
            }

            drawShip(1, scenario.Field1);
            drawShip(2, scenario.Field2);
        }

        private void drawShip(int player, Dictionary<int, int> field)
        {
            for (int i = 0; i < 100; i++)
            {
                if (field.ContainsKey(i))
                {
                    int ship = field[i];
                    UserControl ucShip = new UserControl();
                    int x, y;

                    x = (i % 10) * 30;
                    y = (i / 10) * 30;

                    switch (ship)
                    {
                        case PATROL:
                            ucShip = new ShipPatrolControl();
                            i += 1;
                            break;
                        case DESTROYER:
                            ucShip = new ShipDestroyerControl();
                            i += 2;
                            break;
                        case SUBMARINE:
                            ucShip = new ShipSubmarineControl();
                            i += 2;
                            break;
                        case BATTLESHIP:
                            ucShip = new ShipBattleshipControl();
                            i += 3;
                            break;
                        case CARRIER:
                            ucShip = new ShipCarrierControl();
                            i += 4;
                            break;
                    }

                    ucShip.Margin = new Thickness(x, y, 0, 0);
                    if (player == 1)
                        gridPlayer2.Children.Add(ucShip);
                    else
                        gridPlayer1.Children.Add(ucShip);
                    
                }
            }
        }
    }
}
